// LineStyleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "wgnuplot.h"
#include "src/win/wgnuplib.h"
#include "LineStyleDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLineStyleDlg dialog


CLineStyleDlg::CLineStyleDlg( LPGW lpgw , CWnd* pParent /*=NULL*/)
	: CDialog(CLineStyleDlg::IDD, pParent) , m_pGraphWin( lpgw )
{
	//{{AFX_DATA_INIT(CLineStyleDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CLineStyleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLineStyleDlg)
	DDX_Control( pDX , LS_LINENUM , m_wndLineStyleListBox );
	DDX_Control( pDX , LS_MONOSTYLE , m_wndMonoStyle );
	DDX_Control( pDX , LS_MONOWIDTH , m_wndMonoWidth );

	DDX_Control( pDX , LS_COLORSTYLE , m_wndColorStyle );
	DDX_Control( pDX , LS_COLORWIDTH , m_wndColorWidth );
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CLineStyleDlg, CDialog)
	//{{AFX_MSG_MAP(CLineStyleDlg)
	ON_LBN_SELCHANGE(LS_LINENUM, OnSelchangeLinenum)
	ON_BN_CLICKED(LS_CHOOSECOLOR, OnChoosecolor)
	ON_WM_DRAWITEM()
	ON_BN_CLICKED(LS_DEFAULT, OnDefault)
	ON_EN_CHANGE(LS_COLORWIDTH, OnChangeColorwidth)
	ON_CBN_SELENDOK(LS_COLORSTYLE, OnSelendokColorstyle)
	ON_EN_CHANGE(LS_MONOWIDTH, OnChangeMonowidth)
	ON_CBN_SELENDOK(LS_MONOSTYLE, OnSelendokMonostyle)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLineStyleDlg message handlers

BOOL CLineStyleDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	TCHAR buf[16] = {'\0' ,};
///	LPLS lpls;
///	lpls = (LPLS)GetWindowLong(GetParent(hdlg), 4);

	UINT pen = 2;
	for (int i = 0; i < WGNUMPENS + 2; ++i) 
	{
		if (i==0)
			strcpy(buf,"Border");
		else if (i==1)
			strcpy(buf,"Axis");
		else
			wsprintf(buf,"Line%d",i - 1);

		m_wndLineStyleListBox.AddString( buf );
	}
	m_wndLineStyleListBox.SetCurSel( pen );
	
	static const CString _MONOSTYLE_[] = { "Solid" , "Dash" , "Dot" , "DashDot" , "DashDotDot" };
	UINT nSize = sizeof( _MONOSTYLE_ ) / sizeof( _MONOSTYLE_[0] );
	for( i = 0;i < nSize;++i)
	{
		m_wndMonoStyle.AddString( _MONOSTYLE_[i] );
	}

	if( m_pGraphWin )
	{
		LPLOGPEN plpm = &(m_pGraphWin->monopen[ pen ]);
		m_wndMonoStyle.SetCurSel( plpm->lopnStyle );

		wsprintf(buf,"%d",plpm->lopnWidth.x);
		m_wndMonoWidth.SetWindowText( buf );
	}

	for( i = 0;i < nSize;++i)
	{
		m_wndColorStyle.AddString( _MONOSTYLE_[i] );
	}
	if( m_pGraphWin )
	{
		LPLOGPEN plpc = &(m_pGraphWin->colorpen[ pen ]);
		m_wndColorStyle.SetCurSel( plpc->lopnStyle );

		wsprintf(buf,"%d",plpc->lopnWidth.x);
		m_wndColorWidth.SetWindowText( buf );
	}
				
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

/* force update of owner draw button */
static void UpdateColorSample(HWND hdlg)
{
	RECT rect;
	POINT ptul, ptlr;
	GetWindowRect( GetDlgItem(hdlg, LS_COLORSAMPLE), &rect);
	ptul.x = rect.left;
	ptul.y = rect.top;
	ptlr.x = rect.right;
	ptlr.y = rect.bottom;
	ScreenToClient(hdlg, &ptul);
	ScreenToClient(hdlg, &ptlr);
	rect.left   = ptul.x;
	rect.top    = ptul.y;
	rect.right  = ptlr.x;
	rect.bottom = ptlr.y;
	InvalidateRect(hdlg, &rect, TRUE);

	UpdateWindow(hdlg);
}

static COLORREF GetColor(HWND hwnd, COLORREF ref)
{
	CHOOSECOLOR cc;
	COLORREF aclrCust[16];
	int i;

	for (i=0; i<16; i++) {
		aclrCust[i] = RGB(0,0,0);
	}
	memset(&cc, 0, sizeof(CHOOSECOLOR));
	cc.lStructSize = sizeof(CHOOSECOLOR);
	cc.hwndOwner = hwnd;
	cc.lpCustColors = aclrCust;
	cc.rgbResult = ref;
	cc.Flags = CC_RGBINIT;
	if (ChooseColor(&cc))
		return cc.rgbResult;
	return ref;
}

void CLineStyleDlg::OnSelchangeLinenum() 
{
	TCHAR buf[16] = {'\0' ,};
	// TODO: Add your control notification handler code here
	if( m_pGraphWin )
	{
		UINT pen = m_wndLineStyleListBox.GetCurSel();
		
		LPLOGPEN plpm = &(m_pGraphWin->monopen[ pen ]);
		wsprintf(buf,"%d",plpm->lopnWidth.x);
		m_wndMonoWidth.SetWindowText( buf );
		m_wndMonoStyle.SetCurSel( plpm->lopnStyle );
		
		LPLOGPEN plpc = &(m_pGraphWin->colorpen[ pen ]);
		wsprintf(buf,"%d",plpc->lopnWidth.x);
		m_wndColorWidth.SetWindowText( buf );
		m_wndColorStyle.SetCurSel( plpc->lopnStyle );
		
		UpdateColorSample( m_hWnd );	
	}
}

void CLineStyleDlg::OnChoosecolor() 
{
	// TODO: Add your control notification handler code here
	if( m_pGraphWin )
	{
		UINT pen = m_wndLineStyleListBox.GetCurSel();
		LPLOGPEN plpc = &(m_pGraphWin->colorpen[ pen ]);

		plpc->lopnColor = GetColor( m_hWnd , plpc->lopnColor);
		UpdateColorSample( m_hWnd );
	}
}

void CLineStyleDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

void CLineStyleDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	// TODO: Add your message handler code here and/or call default
	if( m_pGraphWin )
	{
		HBRUSH hBrush;
		LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lpDrawItemStruct;
		UINT pen = m_wndLineStyleListBox.GetCurSel();
		PLOGPEN plpc = &(m_pGraphWin->colorpen[ pen ]);
		hBrush = CreateSolidBrush(plpc->lopnColor);
		FillRect(lpdis->hDC, &lpdis->rcItem, hBrush);
		FrameRect(lpdis->hDC, &lpdis->rcItem, (HBRUSH)GetStockObject(BLACK_BRUSH));
		DeleteObject(hBrush);
	}

	CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

void CLineStyleDlg::OnDefault() 
{
	TCHAR buf[16] = {'\0' ,};
	if( m_pGraphWin )
	{
		PLOGPEN plpm = m_pGraphWin->monopen;
		PLOGPEN plpc = m_pGraphWin->colorpen;
		/* border */
		plpc->lopnColor   = RGB(0,0,0);
		plpc->lopnStyle   = PS_SOLID;
		plpc->lopnWidth.x = 1;
		plpm->lopnStyle   = PS_SOLID;
		plpm->lopnWidth.x = 1;
		plpc++; plpm++;
		/* axis */
		plpc->lopnColor   = RGB(192,192,192);
		plpc->lopnStyle   = PS_DOT;
		plpc->lopnWidth.x = 1;
		plpm->lopnStyle   = PS_DOT;
		plpm->lopnWidth.x = 1;
		/* LineX */
		for (int i = 0; i < WGNUMPENS; i++) 
		{
			plpc++; plpm++;
			plpc->lopnColor   = wginitcolor[ i%WGDEFCOLOR ];
			plpc->lopnStyle   = wginitstyle[ (i/WGDEFCOLOR) % WGDEFSTYLE ];
			plpc->lopnWidth.x = 1;
			plpm->lopnStyle   = wginitstyle[ i%WGDEFSTYLE ];
			plpm->lopnWidth.x = 1;
		}
		/* update window */
		UINT pen = m_wndLineStyleListBox.GetCurSel();
		plpm = &m_pGraphWin->monopen[ pen ];
		plpc = &m_pGraphWin->colorpen[pen];
		m_wndLineStyleListBox.SetCurSel( pen );
		wsprintf(buf,"%d",plpm->lopnWidth.x);
		m_wndMonoWidth.SetWindowText( buf );
		
		m_wndMonoStyle.SetCurSel( plpm->lopnStyle );
		wsprintf(buf,"%d",plpc->lopnWidth.x);
		m_wndColorWidth.SetWindowText( buf );
		
		m_wndColorStyle.SetCurSel( plpc->lopnStyle );
		
		UpdateColorSample( m_hWnd );	
	}
}

void CLineStyleDlg::OnChangeColorwidth() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	if( m_pGraphWin )
	{
		CString rString;
		m_wndColorWidth.GetWindowText( rString);
		
		UINT pen = m_wndLineStyleListBox.GetCurSel();
		PLOGPEN plpc = &(m_pGraphWin->colorpen[ pen ]);
		plpc->lopnWidth.x = atoi( rString );
		if (plpc->lopnWidth.x != 1) 
		{
			plpc->lopnStyle = 0;
			m_wndColorStyle.SetCurSel( plpc->lopnStyle );
		}
	}
}

void CLineStyleDlg::OnSelendokColorstyle() 
{
	// TODO: Add your control notification handler code here
	if( m_pGraphWin )
	{
		UINT pen = m_wndLineStyleListBox.GetCurSel();
		PLOGPEN plpc = &(m_pGraphWin->colorpen[ pen ]);

		plpc->lopnStyle = m_wndColorStyle.GetCurSel();
		if (plpc->lopnStyle != 0) 
		{
			plpc->lopnWidth.x = 1;
			CString rString;
			rString.Format( "%d" , plpc->lopnWidth.x );
			m_wndColorWidth.SetWindowText( rString );
		}
	}
}

void CLineStyleDlg::OnChangeMonowidth() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	if( m_pGraphWin )
	{
		CString rString;
		m_wndMonoWidth.GetWindowText( rString );
		
		UINT pen = m_wndLineStyleListBox.GetCurSel();
		PLOGPEN plpm = &(m_pGraphWin->monopen[ pen ]);
		plpm->lopnWidth.x = atoi( rString );
		
		if (plpm->lopnWidth.x != 1) 
		{
			plpm->lopnStyle = 0;
			m_wndMonoStyle.SetCurSel( plpm->lopnStyle );
		}
	}
}

void CLineStyleDlg::OnSelendokMonostyle() 
{
	// TODO: Add your control notification handler code here
	if( m_pGraphWin )
	{
		UINT pen = m_wndLineStyleListBox.GetCurSel();
		PLOGPEN plpm = &(m_pGraphWin->monopen[ pen ]);

		plpm->lopnStyle = m_wndMonoStyle.GetCurSel();
		if (plpm->lopnStyle != 0) 
		{
			plpm->lopnWidth.x = 1;
			CString rString;
			rString.Format( "%d" , plpm->lopnWidth.x );
			m_wndMonoWidth.SetWindowText( rString );
		}
	}
}
